package com.example.customview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.customview.customview.InputNumberView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements InputNumberView.OnNumberValueChangeListener {

    private static final String TAG = "MainActivity";
    private InputNumberView mInputNumberView = null;
    private final static String UPPER_NUM = "upper";
    private Button mBtn;
    private EditText mEt;
    private CalThread mThread;

    // 创建线程类
    class CalThread extends Thread {
        private Handler mHandler;

        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler() {
                @Override
                public void handleMessage(@NonNull Message msg) {
                    if (msg.what == 0x123) {
                        int upper = msg.getData().getInt(UPPER_NUM);
                        List<Integer> nums = new ArrayList<Integer>();
                        outer:
                        for (int i =2;i<=upper;i++) {
                            int j = 2;
                            while (j<Math.sqrt(i)) {
                                if (i !=2&&1%j== 0) {
                                    continue outer;
                                }
                                j ++;
                            }
                            nums.add(i);
                        }
                        Log.d(TAG,"Toast所在的线程名称"+Thread.currentThread().getName());
                        Toast.makeText(MainActivity.this,nums.toString(),Toast.LENGTH_LONG).show();
                    }
                }
            };
            Looper.loop();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn = findViewById(R.id.btn1);
        mEt = findViewById(R.id.input);
        mThread = new CalThread();
        mThread.start();

        initView();
        initEven();


    }

    private void initView() {
        Log.d(TAG,"UI所在的线程名称"+Thread.currentThread().getName());
        mInputNumberView = findViewById(R.id.input_number_view);
        ViewConfiguration.get(this).getScaledTouchSlop();
    }

    private void initEven() {
        if (mInputNumberView != null) {
            mInputNumberView.setOnNumberValueChangeListener(this);
        }
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Message msg = new Message();
                msg.what = 0x123;
                Bundle bundle = new Bundle();
                bundle.putInt(UPPER_NUM,Integer.parseInt(mEt.getText().toString()));
                msg.setData(bundle);
                mThread.mHandler.sendMessage(msg);
            }
        });
    }

    @Override
    public void onNumberChange(int value) {
        Log.d(TAG," onNumberChange :"+value);
    }
}